package com.eslserver.action.esl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.core.aware.ESLAware;
import com.eslserver.dao.DAOException;
import com.eslserver.dao.ESLServerDAOFactory;
import com.eslserver.dao.ESLDAO;
import com.eslserver.to.ESL;
import com.eslserver.util.JSONResponseUtil;

@SuppressWarnings("serial")
public class LoadESLInfo extends HttpServlet implements ESLAware {
	public static final Logger LOG = LoggerFactory.getLogger(LoadESLInfo.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter writer = resp.getWriter();
		try {
			ESL esl = ESL.createESL();
			String eslID = req.getParameter(PARAM_ESL_ID);
			if (eslID != null && Integer.valueOf(eslID) != 0) {
				ESLDAO eslDAO = ESLServerDAOFactory.getDAOFactory(
						ESLServerDAOFactory.ESLSERVER_DAOFACTORY).getESLDAO();
				esl = eslDAO.loadESL(Integer.parseInt(eslID));
			}
			writer.println(JSONResponseUtil.getJSONObjectString(esl));
		} catch (DAOException e) {
			LOG.error("error laoding ESL Info", e);
		} finally {
			writer.close();
		}
	}
}
